package com.hsnn.medstgmini.pro.plan.controller;

import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.DicSys;
import com.hsnn.medstgmini.base.std.model.StdDrugInfo;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.plan.model.BidHospplan;
import com.hsnn.medstgmini.pro.plan.service.BidHospplandrugManager;
import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.pro.project.service.BidProjectManager;
import com.hsnn.medstgmini.util.DicSysUtil;
import com.hsnn.medstgmini.util.Pagination;

@Controller
@RequestMapping("/bidHospplandrug")
public class BidHospplandrugController {
	
	private static final Logger log = Logger.getLogger(BidHospplandrugController.class);
	@Autowired
	private HibernateSqlUtil UtilDao;
	@Autowired
	private BidHospplandrugManager bidHospplandrugManager;
	@Autowired
	private BidProjectManager bidProjectManager;
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	private Session getSession(){
		Session session = sessionFactory.getCurrentSession();
		if (null == session){
			session = sessionFactory.openSession();
		}
		return session;
	}
	@Autowired
	private  HttpServletRequest request;
	private BidHospplan hospId;
	
	
	@RequestMapping("/toList")
	public String toList(){
		BidProject bp = bidProjectManager.getBidProjectById(request.getParameter("projId"));
		request.setAttribute("projName", bp.getName());
		request.setAttribute("projId", request.getParameter("projId"));
		return "/pro/plan/prodrugCataDetail";
	}
	//未申报
	@RequestMapping("/todrugList")
	public String tooList(){
		List<DicSys> proCatList = DicSysUtil.getDicListByClassCode(Constants.PRO_METHOD);
		BidProject bp = bidProjectManager.getBidProjectById(request.getParameter("projId"));
		request.setAttribute("projName", bp.getName());
		request.setAttribute("proCatList", proCatList);
		request.setAttribute("projId", request.getParameter("projId"));
		return "/pro/plan/drugNotDeclareList";
	}
	
	
	/**
	 * 
	 *@category 获取当前医疗机构申报的药品
	 *@author 
	 *@date 2015年6月12日 下午4:53:01
	 *@return
	 */
	@RequestMapping("/getDrugDataList")
	@ResponseBody
	public Pagination getDrugDataList(){
		Pagination page = new Pagination(request);
		if (null != request.getParameter("drugCode") && !"".equals(request.getParameter("drugCode").trim())){
			page.getConditions().put("drugCode", request.getParameter("drugCode").trim());
		}
		if (null != request.getParameter("drugName") && !"".equals(request.getParameter("drugName").trim())){
			page.getConditions().put("drugName", request.getParameter("drugName").trim());
		}
		if (null != request.getParameter("prodPompId") && !"".equals(request.getParameter("prodPompId").trim())){
			page.getConditions().put("prodPompId", request.getParameter("prodPompId").trim());
		}
		if (null != request.getParameter("projId") && !"".equals(request.getParameter("projId").trim())){
			page.getConditions().put("projId", request.getParameter("projId").trim());
		}
		if (null != request.getParameter("hospId") && !"".equals(request.getParameter("hospId").trim())){
			page.getConditions().put("hospId", request.getParameter("hospId").trim());
		}
		page = bidHospplandrugManager.getDrugDataList(page);
		return page;
	}
	
	@RequestMapping("/deleteStatus")
	@ResponseBody
	public Pagination deleteStatus(StdDrugInfo stdDruginfo) {
		Pagination page = new Pagination();
		try {
			if (stdDruginfo != null && stdDruginfo.getDrugId() != null) {
				stdDruginfo.setUpdUser("xxx");
	//			bidHospplandrugManager.deleteStatus(stdDruginfo);
				
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
//	@RequestMapping("/addDicdrugform" )
//	@ResponseBody
//	public String addDicdrugform(){
//		DicDrugform data = new DicDrugform();
//		data.setFormId(UUID.randomUUID().toString());//生成字符串
//		data.setFormCode(request.getParameter("formcode"));
//		data.setFormName(request.getParameter("formname"));
//		data.setFatherDrugformName(request.getParameter("fathername"));
//		data.setFatherDrugformCode(request.getParameter("fatherid"));
//		data.setStatus(Short.valueOf("1"));//默认启用
//		String info = "";
//		if (request.getParameter("operflag").equals("add")){
//			info = dicDrugformManager.addDicDrugform(data);
//		} else if (request.getParameter("operflag").equals("edit")){
//			data.setFormId(request.getParameter("formid"));
//			info = dicDrugformManager.editDicDrugform(data);
//	 	}
//		return info;
//	}
	
	
	

	
	//删除
	@RequestMapping("/delDrugData")
	@ResponseBody
	public Pagination delDrugData(BidHospplan data){
		Pagination page = new Pagination(request);
		data.setProjPlanId(request.getParameter("projPlanId"));
		try {
			bidHospplandrugManager.delete(data);
			page.setSuccess(true);
		} catch (Exception e) {
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	//批量
	@RequestMapping("/batchChangeStatus")
	@ResponseBody
	public Pagination batchSwitchStatus(String array){
		Pagination page = new Pagination(request);
		try {
			if (array != null && !"".equals(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<BidHospplan>>() {
				}.getType();
				List<BidHospplan> list = gson.fromJson(array, type);
				if (list != null && !list.isEmpty()) {
					for (BidHospplan dp : list) {
						bidHospplandrugManager.delete(dp);
						page.setSuccess(true);
					}
				}
			}
		} catch (Exception e) {
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	
	
	
/*****************/
//	@RequestMapping("/tobidHospplandrug")
//	public String bidHospplandrug(String projplanId){
//		request.setAttribute("projPlanId", projplanId);
//		return "/pro/plan/prodrugCataDetail";
//	}
//	
/*	@ResponseBody
	@RequestMapping("/getInfoBidHospplandrugJson")
	public Pagination getInfoBidHospplandrugJson(String projplanId,String projCode,String hospId,String drugId,String drugName){
		Pagination page = new Pagination(request);
		page.getConditions().put("projplanId", projplanId);
		page.getConditions().put("projCode", projCode);
		page.getConditions().put("hospId", hospId);
		page.getConditions().put("drugId", drugId);
		page.getConditions().put("drugName", drugName);

		return bidHospplandrugManager.getInfoBidHospplandrug(page);
	}
	*/
	

	
	

}
